#Minimum requirement of CMake version : 3.0.0
cmake_minimum_required(VERSION 3.0.0)
cmake_policy(SET CMP0074 NEW)

#Project name and version number
project(COMPASS VERSION 0.1.0)

#set(CMAKE_BUILD_TYPE "Release")
#set(CMAKE_BUILD_TYPE "Debug")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    add_compile_options(-O0)
    add_compile_options(-pthread)
    add_compile_options(-w)
    add_compile_options(-m64)
    add_compile_options(-lz)
    add_compile_options(-lstdc++)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
    add_compile_options(-g)
    add_compile_options(-O0)
    add_compile_options(-Wall)
    add_compile_options(-pthread)
endif()

#if(WIN32)
#    MATH(EXPR stack_size "600*1024*1024")
#    set(CMAKE_EXE_LINKER_FLAGS "-Wl,--stack,${stack_size}")
#endif()

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -pthread")
else()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

#Set the ROOT and subdirectory, you should put the CMakeList.txt in these file directories
set(ROOT ${PROJECT_SOURCE_DIR})
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ROOT}/build/Bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ROOT}/build/Lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ROOT}/build/Lib)

set(CMAKE_DEBUG_POSTFIX "d")
set(CMAKE_RELEASE_POSTFIX "")
set(CMAKE_RELWITHDEBINFO_POSTFIX "rd")
set(CMAKE_MINSIZEREL_POSTFIX "s")

#Choose different compilation configurations according to VS compilation
if(CMAKE_BUILD_TYPE MATCHES "Release")
    set(CMAKE_BUILD_POSTFIX "${CMAKE_RELEASE_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "Debug")
    set(CMAKE_BUILD_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
    set(CMAKE_BUILD_POSTFIX "${CMAKE_RELWITHDEBINFO_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
    set(CMAKE_BUILD_POSTFIX "${CMAKE_MINSIZEREL_POSTFIX}")
else()
    set(CMAKE_BUILD_POSTFIX "")
endif()

#add_subdirectory(thirdparty)
add_definitions(-DENAGLO -DENAGAL -DENACMP -DNEXOBS=3 -DNFREQ=5 -DTRACE)
# global value
# set lib name
set(compassLib     compassLib)
set(so3Lib         so3Lib)
set(insLib         insLib)
set(gnssLib        gnssLib)
set(visioLib       visioLib)
set(fusionLib      fusionLib)
set(getLib         getLib)
#set(guiLib         guiLib)

# set lib path
set( libCompassSrc  ${PROJECT_SOURCE_DIR}/src/compassLib)
set( libSo3Src      ${PROJECT_SOURCE_DIR}/src/so3Lib)
set( libInsSrc      ${PROJECT_SOURCE_DIR}/src/insLib)
set( libGnssSrc     ${PROJECT_SOURCE_DIR}/src/gnssLib)
set( libVisSrc      ${PROJECT_SOURCE_DIR}/src/visioLib)
set( libFusionSrc   ${PROJECT_SOURCE_DIR}/src/fusionLib)
set( libGetSrc      ${PROJECT_SOURCE_DIR}/src/getLib)
#set( libGuiSrc      ${PROJECT_SOURCE_DIR}/src/guiLib)

# For lib source files
add_subdirectory(${libCompassSrc}  ${ROOT}/build/${compassLib}   )
#add_subdirectory(${libSo3Src}      ${ROOT}/build/${so3Lib}       )
add_subdirectory(${libInsSrc}      ${ROOT}/build/${insLib}       )
add_subdirectory(${libGnssSrc}     ${ROOT}/build/${gnssLib}      )
add_subdirectory(${libVisSrc}      ${ROOT}/build/${visioLib}     )
add_subdirectory(${libFusionSrc}   ${ROOT}/build/${fusionLib}    )
add_subdirectory(${libGetSrc}      ${ROOT}/build/${getLib}       )
#add_subdirectory(${libGuiSrc}      ${ROOT}/build/${GuiLib}       )

#CUI APP
set(App COMPASS_APP)
add_subdirectory(${ROOT}/src/app ${ROOT}/build/${App})

# group
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(TARGET ${compassLib} PROPERTY FOLDER "LIB")
#SET_PROPERTY(TARGET ${so3Lib}     PROPERTY FOLDER "LIB")
SET_PROPERTY(TARGET ${insLib}     PROPERTY FOLDER "LIB")
SET_PROPERTY(TARGET ${gnssLib}    PROPERTY FOLDER "LIB")
SET_PROPERTY(TARGET ${visioLib}   PROPERTY FOLDER "LIB")
SET_PROPERTY(TARGET ${fusionLib}  PROPERTY FOLDER "LIB")
SET_PROPERTY(TARGET ${getLib}     PROPERTY FOLDER "LIB")
# SET_PROPERTY(TARGET ${guiLib}     PROPERTY FOLDER "LIB")

#Output Messages for debug the Cmake
message(STATUS "operation system is : ${CMAKE_SYSTEM}")
message(STATUS "current platform is : ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMake version is    : ${CMAKE_SYSTEM_VERSION}")
message(STATUS "C compiler is : ${CMAKE_C_COMPILER}")
message(STATUS "C++ compiler is : ${CMAKE_CXX_COMPILER}")
message(STATUS "The program main directory is : ${ROOT}")